import React, {Fragment} from 'react'
import PropTypes from 'prop-types'
import {routerRedux} from 'dva/router'
import {connect} from 'dva'
import {Button, Card, Col, message, Popconfirm, Row} from 'antd'
import {Page} from 'components'
import queryString from 'query-string'
import List from './components/List'
import Filter from './components/Filter'
import Modal from './components/Modal'


const Goods = ({
                 location, dispatch, goods, loading,
               }) => {
  const {query, pathname} = location
  const {
    list, pagination, currentItem, modalVisible, disable, modalType, selectedRowKeys, productCategory, productBrand, depot,
  } = goods

  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery,
      }),
    }))
  }

  const modalProps = {
    item: modalType === 'create' ? {} : currentItem,
    visible: modalVisible,
    disable,
    width: 820,
    maskClosable: false,
    confirmLoading: loading.effects[`goods/${modalType}`],
    title: `${modalType === 'create' ? '新增商品' : disable ? '商品详情' : '编辑商品'}`,
    wrapClassName: 'vertical-center-modal',
    productCategory,
    productBrand,
    depot,
    onOk (data) {
      if (disable) {
        dispatch({
          type: 'goods/hideModal',
        })
      } else {
        dispatch({
          type: `goods/${modalType}`,
          payload: data,
        })
          .then(() => {
            handleRefresh()
            message.success("操作成功")
          })
      }
    },
    onCancel () {
      dispatch({
        type: 'goods/hideModal',
      })
    },
  }


  const listProps = {
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['goods/query'],
    pagination,
    location,
    handleRefresh,
    onChange (page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize,
      })
    },

    onDeleteItem (id) {
      dispatch({
        type: 'goods/delete',
        payload: id,
      })
        .then(() => {
          handleRefresh({
            current: (list.length === 1 && pagination.current > 1) ? pagination.current - 1 : pagination.current,
          })
          message.success("删除成功")
        })
    },
    onEditItem (item, disable) {
      dispatch({
        type: 'goods/showModal',
        payload: {
          modalType: 'update',
          currentItem: item,
          disable,
        },
      })
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'goods/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
      onSelect: (record) => {
        dispatch({
          type: 'goods/updateState',
          payload: {
            selectRecord: record,
          },
        })
      },
    },
  }

  const filterProps = {
    productCategory,
    productBrand,
    depot,
    filter: {
      ...query,
    },
    onFilterChange (value) {
      handleRefresh({
        ...value,
        current: 1,
      })
    },
    onAdd () {
      dispatch({
        type: 'goods/showModal',
        payload: {
          modalType: 'create',
          disable: false,
        },
      })
    },
  }

  const handleDeleteItems = () => {
    dispatch({
      type: 'goods/multiDelete',
      payload: {
        ids: selectedRowKeys,
      },
    })
      .then(() => {
        handleRefresh({
          current: (list.length === selectedRowKeys.length && pagination.current > 1) ? pagination.current - 1 : pagination.current,
        })
        message.success("商品删除成功")
      })
  }


  return (
    <Fragment>
      <Card style={{marginBottom: 12}}>
        <Filter {...filterProps} />
      </Card>
      <Page inner>
        {
          selectedRowKeys.length > 0 &&
          <Row style={{marginBottom: 24, textAlign: 'right', fontSize: 13}}>
            <Col>
              {`选中了 ${selectedRowKeys.length} 条数据`}
              <Popconfirm title="确定要删除这些数据吗?" placement="left" onConfirm={handleDeleteItems}>
                <Button type="danger" style={{marginLeft: 8}}>删除</Button>
              </Popconfirm>
            </Col>
          </Row>
        }
        <List {...listProps} />
        {modalVisible && <Modal {...modalProps} />}
      </Page>
    </Fragment>
  )
}

Goods.propTypes = {
  employee: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({goods, loading}) => ({goods, loading}))(Goods)
